package net.lee.atomixdemo;


import io.atomix.cluster.Member;

import io.atomix.core.Atomix;
import io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroup;
import io.atomix.protocols.raft.partition.RaftPartitionGroup;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Collection;
import java.util.Collections;

@SpringBootApplication
public class AtomixDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(AtomixDemoApplication.class, args);

        String portStr="";
        if(args.length>0)
        {
            portStr = args[0];
        }

        if(StringUtils.isBlank(portStr))
        {
            portStr="member-1";
        }
        Atomix atomix = ((Atomix.Builder) (Atomix.builder()
                .withLocalMember(portStr)
                .withMembers(
                        Member.builder("member-1")
                                .withType(Member.Type.PERSISTENT)
                                .withAddress("localhost:5001")
                                .build(),
                        Member.builder("member-2")
                                .withType(Member.Type.PERSISTENT)
                                .withAddress("localhost:5002")
                                .build(),
                        Member.builder("member-3")
                                .withType(Member.Type.PERSISTENT)
                                .withAddress("localhost:5003")
                                .build())))
                .withManagementGroup(RaftPartitionGroup.builder("system")
                        .withNumPartitions(1)
                        .withMembers("member-1", "member-2", "member-3")
                        .build())
                .withPartitionGroups(
                        PrimaryBackupPartitionGroup.builder("data")
                                .withNumPartitions(32)
                                .build())
                .build();
       // Collection<Node> nodes = atomix.clusterService().getNodes();
        Collection<Member> memberCollections = atomix.membershipService().getMembers();

        atomix.start().join();
        System.out.println("-----------------"+portStr+"-----start"+"----------------------------");
    }
}
